<template>
    <div class="container">
        <!-- header -->
        <div class="headerBox">
            <div class="mdle">
                <span class="header-title">选择学校</span>
                <el-select
                    v-model="schoolId"
                    filterable
                    placeholder="请选择"
                    size="small"
                    @change="getTableData(1)"
                >
                    <el-option
                        v-for="item in optionSchool"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </div>
            <div class="mdle">
                <span class="header-title">状态</span>
                <el-select
                    v-model="status"
                    filterable
                    placeholder="请选择"
                    size="small"
                    @change="getTableData(1)"
                >
                    <el-option
                        v-for="item in optionStatus"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </div>
            <div class="mdle">
                <span class="header-title">用户:</span>
                <el-input
                    v-model="phoneNum"
                    placeholder="用户ID或手机号"
                    suffix-icon="el-icon-search"
                    size="small"
                    class="cont-ipt"
                    clearable
                    @change="getTableData(1)"
                    @keyup.native.enter="getTableData(1)"
                />
            </div>
            <div class="mdle">
                <span class="header-title">内容文字搜索:</span>
                <el-input
                    v-model="content"
                    placeholder="请输入内容文字"
                    suffix-icon="el-icon-search"
                    size="small"
                    class="cont-ipt"
                    clearable
                    @change="getTableData(1)"
                    @keyup.native.enter="getTableData(1)"
                />
            </div>
            <div class="mdle">
                <span class="header-title">选择时间:</span>
                <el-date-picker
                    v-model="create_time"
                    type="datetimerange"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    size="small"
                />
            </div>
            <div class="mdle">
                <el-button
                    type="primary"
                    size="small"
                    @click="getTableData(1)"
                >搜索</el-button>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
        >
            <el-table-column
                label="ID"
                type="index"
                align="center"
            />
            <el-table-column
                prop="stuagent_invite_code"
                label="邀请码"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="student.true_name"
                label="掌上精英姓名"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="120"
            />
            <el-table-column
                prop="student.student_no"
                label="用户ID"
                :width="160"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="student.phone"
                label="手机号"
                :width="120"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="student.school"
                label="学校名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="content"
                label="内容文字"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="images"
                label="内容图片"
                show-overflow-tooltip
                align="center"
            >
                <template slot-scope="scope">
                    <template v-if="scope.row.images.length > 0">
                        <a
                            href="javascript:;"
                            style="color: #409EFF"
                            @click="fnShowPreview(scope.row.images)">查看全部图片({{ scope.row.images.length }})</a>
                    </template>
                    <template v-else>
                        --
                    </template>
                </template>
            </el-table-column>
            <el-table-column
                prop="create_time"
                label="创建时间"
                show-overflow-tooltip
                align="center"
            >
                <template slot-scope="scope">
                    <span>{{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="is_accept"
                label="状态"
                show-overflow-tooltip
                align="center"
            >
                <template slot-scope="scope">
                    <span v-if="scope.row.accept_status == 1">已采纳</span>
                    <span v-else-if="scope.row.accept_status == 0">未采纳</span>
                    <span v-else-if="scope.row.accept_status == -1">待批阅</span>
                    <span v-else>--</span>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                fixed="right"
                align="center"
                :width="130"
            >
                <template slot-scope="scope">
                    <el-button
                        :disabled="scope.row.accept_status == 0"
                        type="text"
                        @click="fnNotAccept(scope.row)">不采纳</el-button>
                    <el-button
                        :disabled="scope.row.accept_status == 1"
                        type="text"
                        @click="fnAccept(scope.row)">采纳</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- 分页 -->
        <div
            class="pagination-wrapper clearfix"
            v-if="tableData.length > 0"
        >
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>
        <!-- /分页 -->

        <img-preview ref="preview" />

    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';
import {getDate} from '@/assets/js/utils';
export default {
    components: {
        imgPreview
    },
    data () {
        return {
            create_time: '',
            dialogVisible:false,
            optionSchool: [{
                value: '',
                label: '全部'
            }],
            phoneNum:'',
            content:'',
            status:'99',
            cityId:'',
            schoolId:'',
            optionStatus: [{
                value: '99',
                label: '全部'
            },{
                value: 1,
                label: '已采纳'
            },{
                value: 0,
                label: '未采纳'
            },{
                value: -1,
                label: '待批阅'
            }],
            tableData: [],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
        };
    },
    created () {
        this.setHeader();
        this.getSchoolList();
        this.getTableData();
    },
    methods: {
        /**
         * 设置表单header
         */
        setHeader () {
            this.$store.commit('SET_HEADER', [{
                title: "APP后台",
            },
            {
                title: '掌大精英'
            },
            {
                title: "掌大精英动态"
            }]
            );
        },

        /**
         * 查看图片
         */
        fnShowPreview(images) {
            this.$refs.preview.show(images);
        },

        /**
         * 获取已开通学校列表
         */
        getSchoolList () {
            this.$post('/student/forum%5Cget_school_list', {
            }, resp => {
                if (resp.code == 1) {
                    let list = [];

                    resp.data.list.map(item => {
                        list.push({
                            value: item.school_id,
                            label: item.school_name
                        });
                    });

                    this.optionSchool = this.optionSchool.concat(list);
                }
            });
        },

        /**
         * 获取表格数据
         */
        getTableData (page) {
            let parameter = this.$route.query;

            this.$post('/student/stuagent%5Cget_post_list', {
                page_id: page || this.page.now_page,
                accept_status: this.status,
                content: this.content,
                city_id: this.cityId,
                school_id: this.schoolId,
                keyword_student_id: parameter.phone || this.phoneNum,
                date_range: this.create_time === null ? '' : getDate(new Date(this.create_time[0])) + '-' + getDate(new Date(this.create_time[1]))
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) {
                return '--';
            }
            return val;
        },
        /**
         * 采纳
         */
        fnAccept(row){
            this.$confirm('采纳后将增加该用户对应的EI分值，确认采纳？', '点击采纳弹出', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/stuagent%5Cset_post_accept_status', {
                    status: 1,
                    post_no: row.post_no,
                }, resp => {
                    if (resp.code == 1) {
                        this.$message({
                            type: 'success',
                            message: '采纳成功',
                            duration: 1000
                        });
                        this.getTableData(this.page.now_page);
                    }else if(resp.code == 0){
                        this.$message({
                            type: 'error',
                            message: '采纳失败',
                            duration: 1000
                        });
                    }
                });
            }).catch(() => {});
        },
        /**
         * 取消采纳
         */
        fnNotAccept(row){
            this.$confirm('取消采纳后将减掉该用户对应的EI分值，是否取消采纳？', '点击取消采纳弹出', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/stuagent%5Cset_post_accept_status', {
                    status: 2,
                    post_no: row.post_no,
                }, resp => {
                    if (resp.code == 1) {
                        this.$message({
                            type: 'success',
                            message: '取消成功',
                            duration: 1000
                        });
                        this.getTableData(this.page.now_page);
                    } else if(resp.code == 0){
                        this.$message({
                            type: 'error',
                            message: '取消失败',
                            duration: 1000
                        });
                    }
                });
            }).catch(() => {});
        },
        /**
         * 关闭弹窗
         */
        handleClose(){
            this.dialogVisible = false;
        },
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 5px 15px 15px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .headerBox {
            margin-bottom: 10px;
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            .mdle {
                display: flex;
                height: 32px;
                align-items: center;
                margin-top: 10px;
            }
            .header-title {
                color: #606266;
                margin-right: 8px;
                white-space: nowrap;
            }
            .el-input{
                width: 200px;
            }
        }
        .pagination-wrapper {
            padding-top: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
        .cont-comment {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        .dialog-footer {
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .curse-ford {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
    }
    .mdle{
        margin-right: 30px;
    }
</style>
<style lang="less">
    .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
</style>